
/*---------------------------------------------------------------警力查询-----------------------------------------------------------------------------*/
var areaArr 	  = []; //区数组
var typeArr		  = []; //警力类型
var departmentArr = []; //单位数组
function upLoad(){
	$.ajax({
		type:"POST",
		url:"public/query/query.php",
		cache:false,
		data:"act=jl&control=liandong&t="+Math.random(),
		success:function(str){
			var arr = str.split('*');
			var areaStrArr = arr[0].split('|');
			for(var i=0;i<areaStrArr.length-1;i++){
				//将区写入数组
				var StrArr = areaStrArr[i].split('#');
				areaArr.push(StrArr[0]);
				//单位数组
				var unitsArr = StrArr[1].split(',');
				unitsArr.length = unitsArr.length-1;
				departmentArr.push({'area':StrArr[0], 'units':unitsArr, center:[StrArr[2], StrArr[3]]});
			}
			//将警力类型写入数组
			typeArr = arr[1].split(',');
			typeArr.length = typeArr.length-1; //将最后一个空的去掉
			//将数组遍历到弹出框的select中
			AddSelectOption(areaArr, 'area');
			AddSelectOption(typeArr, 'type');
		}
	});
}
function PolicePower(){
	mapObj.removeAllOverlays();
	isClearSetTimeOut = true;
	areaArr.length = 0; //添加下拉框选项前先清空数组
	SetMapCenter(OnloadCenterLng, OnloadCenterLat);
	SetZoom(OnloadZoom);
	SetMapCenter(OnloadCenterLng, OnloadCenterLat);
	upLoad();
	var temp = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="border-bottom:1px solid #000;">查询指定人员或车辆的相关信息：</td></tr><tr><td>查询方式：<input type="radio" checked="checked" id="male" name="term" value="male" onclick="selectTerm(this.value)"><lable for="male">按条件查</lable> <input type="radio" name="term" value="female" id="female" onclick="selectTerm(this.value)"><lable for="female">直接输入</lable><td></tr><tr><td id="comment"><div id="maleDiv" style="position:relative;">所属区域：';
	
	
		
	temp+='<select id="area" style="width:60px" onchange="UnitOfArea(this.value);this.nextSibling.value=this.value"><option value="选择区">选择区</option></select><input type="text" value="选择区" id="input_1" onKeyUp="text_keyup_2(\'div_1\',this)" style="z-index:10000;width:42px;position:absolute;top:3px;left:60px;"><div style="display:block; top:28px; left:60px;position:absolute; z-index:10000;background:#06F;" id="div_1"></div>';

	//temp+='<select id="units"  onchange="this.nextSibling.value=this.value"  style="width:100px"><option value="选择单位">选择单位</option></select><input type="text" value="选择单位" id="input_2" onKeyUp="text_keyup_3(\'div_2\',this)" style="z-index:10000;width:86px;position:absolute;top:3px;left:119px;"><div style="display:block; top:28px; left:118px;position:absolute; z-index:10000;background:#06F;" id="div_2"></div>';
	
	
	
	//temp+='<select id="area" style="width:60px;" onchange="UnitOfArea(this.value)"><option value="区域">区域</option></select>';
	
	
	
	temp+='<select id="units" style="width:100px;" onchange="UnitOfType(\'units\');this.nextSibling.value=this.value"><option value="单位">单位</option></select><input type="text" value="选择单位" id="input_2" onKeyUp="text_keyup_3(\'div_2\',this)" style="z-index:10000;width:86px;position:absolute;top:3px;left:119px;"><div style="display:block; top:28px; left:118px;position:absolute; z-index:10000;background:#06F;" id="div_2"></div>';
	
	
	
	temp+='<select id="type" onchange="UnitOfType(\'type\')"><option value="类型">类型</option></select>';
    
	
	temp+='<br />选择车辆：<select id="policeNo" disabled="disabled" style="width:100px"><option value="选择">选择</option></select></div><div id="femaleDiv" style="display:none">查询条件：<input type="radio" id="ByPlatenum" name="By" value="车牌" onclick="SelectQueryBy(\'输入车牌\')" checked="checked" /> 车牌号 <input type="radio" id="ByManNum" name="By" value="人员号" onclick="SelectQueryBy(\'输入人员号\')" /> 人员号 <br /><label id="selecttype">输入车牌</label>：<input type="text" id="querywords" name="querywords" value="豫A" style="width:180px; color:#666" /></div></td></tr><tr><td align="right"><input name="" type="button" class="btnbg" value="查询信息" style="width:70px;" onclick="QueryPolicePower()"/> <input name="" type="button" class="btnbg" value="地图标注" style="width:70px;" onclick="QueryAndMarker()"/></td></tr><tr><td align="left"><div id="QueryResultPolice"></div></td></tr></table>'
	showMapMenu('车辆查询',temp);
}
//查询条件的显示与隐藏

/*-----------------------------------------------------------------------------------------------------------------------------------*/


function text_keyup_3(div,obj){
if(obj.previousSibling.options.length==1){
	obj.value="选择单位";
	return 0;
	}
if(obj.value==""){
		return 0;
		}

var n=0;
	for(var i=1; i<document.getElementById('units').options.length;i++){
	   if(document.getElementById('units').options[i].text==obj.value){
		   document.getElementById('units').options[i].selected=true;
		   UnitOfType('1');
		   n=1;
		   }
	}
	if(n==1){
		return 0;
		}

	$.ajax({
			type:"POST",
			url:"public/Location/key_up.php",
			cache:false,
			data:"a="+document.getElementById('area').value+"&text="+obj.value,
			success:function(data){
		         if(data!=""&&data.split("_").length>0)
			     {
				 out_list(data);
				 }
				 else{
				document.getElementById(div).innerHTML="";
				}
			}
		});
	function out_list(value){
	var out="<table width='100px' style\"border-width:1px; border-color:#4c62ff; border-style:solid; \">";
	out+="<tr><td style=\"height:0px\"></td></tr>";
	for(var i=0;i<value.split("_").length;++i){
	out=out+"<tr><td onclick=\"sub_a(this.title,'input_2','div_2')\" title='"+value.split("_")[i]+"' style=\"cursor:pointer;\" onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\">"+value.split("_")[i]+"</td></tr>";
	}
	out=out+"<tr><td onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\" style=\"text-align:right;cursor:pointer;\" onclick=\"close_div('div_2')\">关闭</td></tr>";
	out=out+"</table>";
	obj1=document.getElementById("div_2");
	obj1.innerHTML=out;
		}
		
	}
function text_keyup_2(div,obj){
	var n=0;
	for(var i=1; i<document.getElementById('area').options.length;i++){
	   if(document.getElementById('area').options[i].text==obj.value){
		   document.getElementById('area').options[i].selected=true;
		     UnitOfArea(obj.value);
		   n=1;
		   }
	}
	if(n==1){
		return 0;
		}
	if(obj.value==""){
		return 0;
		}
			$.ajax({
			type:"POST",
			url:"public/Location/key_up.php",
			cache:false,
			data:"text="+obj.value,
			success:function(data){
		         if(data!=""&&data.split("_").length>0)
			     {
				 out_list(data);
				 }
				 else{
				document.getElementById(div).innerHTML="";
				}
			}
		});
	

	function out_list(value){
		var out="<table width='60px' style\"border-width:1px; border-color:#4c62ff; border-style:solid; \">";
		out+="<tr><td style=\"height:0px\"></td></tr>";
	for(var i=0;i<value.split("_").length;++i){
	out=out+"<tr><td onclick=\"sub_a(this.title,'input_1','div_1')\" title='"+value.split("_")[i]+"' style=\"cursor:pointer;\" onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\">"+value.split("_")[i]+"</td></tr>";
	}
	out=out+"<tr><td onmouseover=\"this.style.backgroundColor='green'\" onmouseout=\"this.style.backgroundColor=''\" style=\"text-align:right;cursor:pointer;\" onclick=\"close_div('div_1')\">关闭</td></tr>";
	out=out+"</table>";
	obj1=document.getElementById("div_1");
	obj1.innerHTML=out;
		}
	//document.getElementById(div).innerHTML="sdfsdf";
	}
	
function sub_a(text,a,b){
	document.getElementById(a).value=text;
		if(b=='div_2'){
	for(var i=1; i<document.getElementById('units').options.length;i++){
	   if(document.getElementById('units').options[i].text==text){
		   document.getElementById('units').options[i].selected=true;
		     UnitOfType('1');
		   }
	}
		}else{
			for(var i=1; i<document.getElementById('area').options.length;i++){
	   if(document.getElementById('area').options[i].text==text){
		   document.getElementById('area').options[i].selected=true;
		   UnitOfArea(text)
		   }
	}
	}
	close_div(b);
	}
function close_div(a){
	var obj=document.getElementById(a);
	obj.innerHTML="";
	}













/*-----------------------------------------------------------------------------------------------------------------------------------*/











function selectTerm(termId){
	if(termId=="female"){
		document.getElementById("femaleDiv").style.display="";
		document.getElementById("maleDiv").style.display="none";
	}else if(termId=='male'){
		document.getElementById("area").length = 1;
		document.getElementById('units').length = 1;
		document.getElementById('type').length = 1;
		document.getElementById('policeNo').length = 1;
		document.getElementById("femaleDiv").style.display="none";
		document.getElementById("maleDiv").style.display="";
	}
}



//区与单位的联动
function UnitOfArea(area){
				document.getElementById("div_1").innerHTML="";
	document.getElementById("div_2").innerHTML="";
	//根据每个区的名字将单位写进数组
	for(var i=0; i<departmentArr.length; i++){
		if(departmentArr[i].area == area){
			var unitsArr = departmentArr[i].units;
		}
	}
	//每次选择前清空选择单位
	document.getElementById('units').options.length = 1
	AddSelectOption(unitsArr, 'units')
}

//选择类型后，显示该单位下的所以车辆信息、警员信息
function UnitOfType(selectID){
	if(selectID!="type"){
		return 0;
		}
	if(document.getElementById('type').value=="类型"){
		document.getElementById('policeNo').disabled=true;
		return 0;
		}
	document.getElementById("div_1").innerHTML="";
	document.getElementById("div_2").innerHTML="";
	document.getElementById('QueryResultPolice').innerHTML ="";
	document.getElementById('policeNo').length = 1;
	var area = document.getElementById('area').value;
	var type = document.getElementById('type').value;
	var units = document.getElementById('units').value;
	//判断是否选择了区和单位
	if(area == '区域'){
		document.getElementById('QueryResultPolice').innerHTML = '<font color="red">请先选择区</font>';
		return false;
	}else if(units == '单位'){
		document.getElementById('QueryResultPolice').innerHTML = '<font color="red">请先选择单位</font>';
		return false;
	}

	//假如一切正常，就请求所有设备
	$.ajax({
		type:"POST",
		url:"public/query/query.php",
		cache:false,
		dataType:'json',
		data:"act=jl&control=policeData&area="+area+"&units="+units+"&type="+type+"&t="+Math.random(),
		success:function(data){
			if(data.status){
				var str='<font color="red">'+data.info+'</font>';
				document.getElementById('QueryResultPolice').innerHTML = str;
				
			}else if(type=="人员"){//personnel_index/personnel_sn
				for(var i=0;i<data.length;i++){
					var oOption=document.createElement('OPTION');
					oOption.text=data[i].PERSONNEL_SN;
					oOption.value=data[i].PERSONNEL_INDEX;
					document.getElementById('policeNo').options.add(oOption);
				}
				//设置警力可以选择
				document.getElementById('policeNo').disabled=false;
			}else{
				for(var i=0;i<data.length;i++){
					var oOption=document.createElement('OPTION');
					oOption.text=data[i].LICENSE_PLATE;
					oOption.value=data[i].VEHICLE_INDEX;
					document.getElementById('policeNo').options.add(oOption);
				}
				//设置警力可以选择
				document.getElementById('policeNo').disabled=false;
			}
		}
	});
}

//遍历数组添加选项
function AddSelectOption(array, selectid){
	if(document.getElementById(selectid).options.length = 1){
		if(array){
			for( var i=0;i<array.length;i++){
				var oOption = document.createElement('OPTION');
				oOption.text = array[i];
				oOption.value = array[i];
				document.getElementById(selectid).options.add(oOption); 
			}
		}
	}
}

//选择条件显示不同提示
function SelectQueryBy(selecttypewords){
	document.getElementById('selecttype').innerHTML = selecttypewords;
	selecttypewords == '输入车牌'?document.getElementById('querywords').value ='豫A':document.getElementById('querywords').value ='';
}
//从数组中查询警力信息
var resultObj;
var resu;
var str;
var type;
function QueryPolicePower(){
	var QueryWords;	//查询的车牌号或警号
	var QueryBy;	//选择的车牌号还是警号的标志
	if(document.getElementById("male").checked){//按条件查询
		var sel=document.getElementById("policeNo");
		QueryWords=sel.options[sel.selectedIndex].text;
		QueryBy=document.getElementById("type").value;
		if(QueryWords=="选择"&&QueryBy=="类型"){
			document.getElementById('QueryResultPolice').innerHTML = '<font color="red">请选择车牌号，包括车辆或摩托车</font>';
			return false;
		}
	}else if(document.getElementById("female").checked){//直接输入查询
		var QueryWords = document.getElementById('querywords').value.toUpperCase();	//查询的车牌号或警号
		var QueryBy	   = document.getElementById('selecttype').innerHTML ;	//查询的条件，按车牌、按警号
		//判断是否为空
		if(QueryBy == '输入车牌'){
			if(QueryWords == '' || QueryWords == '豫A'){
				document.getElementById('QueryResultPolice').innerHTML = '<font color="red">请输入车牌号，包括车辆或摩托车</font>';
				return false;
			}
		}else{
			if(QueryWords == ''){
				document.getElementById('QueryResultPolice').innerHTML = '<font color="red">请输入人员编号</font>';
				return false;
			}
		}	
	}
	select();
	//查询的车辆或人员相关信息
	function select(){
		var strmessage = '<font color="red">正在查询中，请稍等！</font>';
		$("#QueryResultPolice").html(strmessage);
		$.ajax({
			type:"POST",
			url:"public/query/query.php",
			cache:false,
			dataType:'json',
			data:"act=jl&control=select&By="+QueryBy+"&word="+QueryWords+"&t="+Math.random(),
			success:function(obj){
				//var obj = eval(data);
				if(obj.status){
					str = '<font color="red">'+obj.info+'</font>'
					$("#QueryResultPolice").html(str);
					return false;
				}else{
					var QuName;
					if(obj[0].VEHICLE_TYPE_NAME){
						QuName=obj[0].VEHICLE_TYPE_NAME;
					}else{
						QuName="人员";
					}
					resu=[obj[0].LONGITUDE, obj[0].LATITUDE];
					if(!obj){
						str = '<font color="red">暂无相关信息，请确认您的输入！</font>';
					}else{
						type=obj[0].VEHICLE_TYPE_NAME;
						if(QueryBy == '输入车牌'||QueryBy=="车辆"||QueryBy=="摩托"){
							var mapLngLat=gpsToMap(obj[0].LONGITUDE,obj[0].LATITUDE);
							var mapLngLatArr=mapLngLat.split(',');
							QueryInfo(mapLngLatArr[0],mapLngLatArr[1]);
							str = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="font-weight:bold">车辆&nbsp;'+QueryWords+'&nbsp;相关信息：</td></tr><tr><td>当前位置：<span id="Qinfo"></span>&nbsp;<font color="blue"></font></td></tr><tr><td>所属区域：'+obj[0].AREA_NAME+'</td></tr><tr><td>所属单位：'+obj[0].DEPARTMENT_NAME+'</td></tr><tr><td>车辆类型：'+obj[0].VEHICLE_TYPE_NAME+'</td></tr><tr><td>手机号：'+obj[0].SIM+'</td></tr><tr><td>速度：'+(obj[0].SPEED*1.842).toFixed(2)+'km/h</td></tr><tr><td>方向：'+obj[0].ANGLE+'</td></tr><tr><td>高度：'+(obj[0].HEIGHT)+'m</td></tr></table>';
							
						}else{
							var mapLngLat=gpsToMap(obj[0].LONGITUDE,obj[0].LATITUDE);
							var mapLngLatArr=mapLngLat.split(',');
							QueryInfo(mapLngLatArr[0],mapLngLatArr[1]);
							str = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="font-weight:bold">人员&nbsp;'+QueryWords+'&nbsp;相关信息：</td></tr><tr><td>当前位置：<span id="Qinfo"></span>&nbsp;<font color="blue"></font></td></tr><tr><td>人员编号：'+obj[0].PERSONNEL_SN+'</td></tr><tr><td>人员姓名：'+obj[0].PERSONNEL_NAME+'</td></tr><tr><td>所属区域：'+obj[0].AREA_NAME+'</td></tr><tr><td>所属单位：'+obj[0].DEPARTMENT_NAME+'</td></tr><tr><td>手机号：'+obj[0].SIM+'</td></tr><tr><td>速度：'+(obj[0].SPEED*1.842).toFixed(2)+'km/h</td></tr><tr><td>方向：'+obj[0].ANGLE+'</td></tr><tr><td>高度：'+(obj[0].HEIGHT)+'m</td></tr></table>';
						}
					}
					$("#QueryResultPolice").html(str);
					resultObj=[{lng:obj[0].LONGITUDE, lat:obj[0].LATITUDE, type:QuName,con:str}];
				}
			}
		});
	}
	//setInterval(select,10000);
}

//由经纬度坐标得到详细地址信息
function QueryInfo(lng,lat){
	var mls =new MReGeoCodeSearch(); 
	var mlsp= new MReGeoCodeSearchOptions(); 
	mlsp.resType='JSON';
	mls.setCallbackFunction(myfunc); 
	mls.poiToAddressSimple(new MLngLat(lng,lat),mlsp); 
	function myfunc(data){
		if(data.error_message != undefined){ 
		  alert(data.error_message); 
		}else{   
			//解析返回数据 
			var location=data.message.split(";");
			$('#Qinfo').html(location[2]);
		} 
	}
}

//模拟查询后并在地图上标注
function QueryAndMarker(){
	if(resultObj){
		//Markers([resultObj[0]],str);
		pantoPoint(resultObj[0].lng,resultObj[0].lat);
		switch(type){
			case '车辆':
				var imageUrl = 'images/ico_car.gif';
				break;
			case '摩托':
				var imageUrl = 'images/ico_moto.gif';
				break;
			default:
				var imageUrl='images/ico_man.gif';
		}
		SingleMarker('LocationDirection',resultObj[0].lng,resultObj[0].lat, false, MIDDLE_CENTER, [0,0], imageUrl, 0);

	}else{
		$('QueryResultPolice').html('<font color="red">请先查询以后才能标记</font>');
	}
}

/*---------------------------------------------------------------出警查询-----------------------------------------------------------------------------*/

function GoTask(){
	mapObj.removeAllOverlays();
	var temp = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="border-bottom:1px solid #000;">查询指定指定时间段内的车辆信息：</td></tr><tr><td>开始时间：<input id="StartTime" type="text" name="StartTime" class="Wdate" onfocus="WdatePicker({dateFmt:\'yyyy-MM-dd HH:mm:ss\'})" style="width:184px" /></td></tr><tr><td>结束时间：<input type="text" id="EndTime" name="EndTime" class="Wdate" onfocus="WdatePicker({dateFmt:\'yyyy-MM-dd HH:mm:ss\'})" style="width:184px" /></td></tr><tr><td align="right"><input name="" type="button" class="btnbg" value="车辆信息" style="width:70px;" onclick="QueryGoTask()"/></td></tr><tr><td align="left"><div id="QueryResultTask"></div></td></tr></table>';
	showMapMenu('出辆查询',temp);
}

function QueryGoTask(){
	mapObj.removeOverlaysByType(MOverlay.TYPE_POLYLINE);		//画出轨迹前删除原有轨迹
	$('#QueryResultTask').html();	//先清空原来有的提示无信息内容
	
	//判断选择的时间是否符合要求
	var StartTime = $('#StartTime').val();
	var EndTime = $('#EndTime').val();
	if(StartTime == ''){
		$('#QueryResultTask').html('<font color="red">请选择开始时间</font>');
		return false;
	}else if(EndTime == ''){
		$('#QueryResultTask').html('<font color="red">请选择结束时间</font>');
		return false;
	}else{
		var sd = new Date(StartTime.replace(/-/g,"\/"));
		var ed = new Date(EndTime.replace(/-/g,"\/"));
		if(sd>ed){
			$('QueryResultTask').html('<font color="red">开始时间不能大于结束时间</font>');
			return false;
		}else{
			
			$.ajax({
				type:"POST",
				url:"public/query/query.php",
				data:"act=cj&src=one&sdate="+StartTime+"&edate="+EndTime+"&t="+Math.random(),
				success:function(data){
					$('#QueryResultTask').html(data);
				}
			});
		}
	}
}

//处理选择出警任务人或车
function ChangeSrc(selectId){
	var key=selectId.substr(3,1);
	var sd='sd'+key;
	var ed='ed'+key;
	var StartTime = $("#"+sd).html();
	var EndTime = $("#"+ed).html();
	var valu=$("#"+selectId).val();
	if(valu=="请选择人员"||valu=="请选择车辆"){
		$("#message").html("<font color='red'>请选择车辆或人员</font>");
	}else{
		$("#message").html("");
		$.ajax({
			type:"POST",
			url:"public/query/query.php",
			dataType:'json',
			data:"act=cj&src=two&type="+selectId+"&id="+valu+"&sdate="+StartTime+"&edate="+EndTime+"&t="+Math.random(),
			success:function(data){
				var PointArr = [];
				//根据出警名称查询该任务路线
				$.each(data,function(index,array){
					/*var str=gpsToMap(array['LONGITUDE'], array['LATITUDE']);
					var array=str.split(',');
					Lng=array[0];
					Lat=array[1];
					PointArr.push(new MLngLat(Lng, Lat));*/
					//PointArr.push(new MLngLat(array['LONGITUDE'], array['LATITUDE']));
					PointArr.push([array['LONGITUDE'], array['LATITUDE']]);
				});
				//画出轨迹前删除原有轨迹
				mapObj.removeOverlaysByType(MOverlay.TYPE_POLYLINE);
				//画出轨迹
				addMPolyline(PointArr);
				pantoPoint(PointArr[PointArr.length-1]);
			}
		});
	}
}

/*---------------------------------------------------------------案件查询-----------------------------------------------------------------------------*/

function TaskName(){
	mapObj.removeAllOverlays();
	$.ajax({
		type:"POST",
		url:"public/query/query.php",
		data:"act=aj&src=all&t="+Math.random(),
		success:function(data){
			showMapMenu('事件查询',data);
		}
	});
}

//处理选择案件事件
function changeName(){
	//画出轨迹前删除原有轨迹
	mapObj.removeOverlaysByType(MOverlay.TYPE_POLYLINE);
	var valu=$("#aj").val();
	if(valu=="请选择案件名称"){
		$("#QueryResultTaskName").html("<font color='red'>请选择案件名称</font>");
	}else{
		$.ajax({
			type:"POST",
			url:"public/query/query.php",
			data:"act=aj&src=one&value="+valu+"&t="+Math.random(),
			success:function(data){
				$('#Task').html(data);
			}
		});
	}
}

//处理选择案件
function Change(selectId){
	var valu=$("#"+selectId).val();
	if(valu=="请选择人员"||valu=="请选择车辆"){
		$("#message").html("<font color='red'>请选择车辆或人员</font>");
	}else{
		$("#message").html("");
		$.ajax({
			type:"POST",
			url:"public/query/query.php",
			data:"act=aj&src=two&type="+selectId+"&id="+valu+"&t="+Math.random(),
			success:function(data){
				var data=eval(data);
				var PointArr = [];
				//根据出警名称查询该任务路线
				$.each(data,function(index,array){
					PointArr.push([array['LONGITUDE'], array['LATITUDE']]);
				});
				//画出轨迹前删除原有轨迹
				mapObj.removeOverlaysByType(MOverlay.TYPE_POLYLINE);
				//画出轨迹
				addMPolyline(PointArr);
				pantoPoint(PointArr[PointArr.length-1]);
			}
		});
	}
}